package com.freight.order.serviceimpl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.freight.base.utils.AreaAddressUtil;
import com.freight.core.common.constant.CommonConstant;
import com.freight.core.common.exception.FreightException;
import com.freight.core.common.utils.ObjectUtil;
import com.freight.core.common.utils.SecurityUtil;
import com.freight.core.entity.User;
import com.freight.order.dao.CommonLineDao;
import com.freight.order.dto.CommonLineDto;
import com.freight.order.entity.CommonLine;
import com.freight.order.mapper.CommonMapper;
import com.freight.order.service.PccCommonLineService;
import com.freight.order.vo.CommonLineListVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Slf4j
@Service
@Transactional
public class PccCommonLineServiceImpl implements PccCommonLineService {

    @Autowired
    private AreaAddressUtil areaAddressUtil;

    @Autowired
    private CommonMapper commonMapper;

    @Autowired
    private CommonLineDao commonLineDao;

    @Autowired
    private SecurityUtil securityUtil;

    @Override
    public Page<CommonLineListVo> getCommonLinePage(Page<CommonLineListVo > page, Long commonId, String beginAreaCode, String endAreaCode, String companyPhone, String companyName) {
        User user=securityUtil.getCurrUser();
        Page<CommonLineListVo> list= commonMapper.getPlCommonLinePage(page,commonId,  beginAreaCode,  endAreaCode, companyPhone,  companyName);
        areaAddressUtil.fillAreaAddress(list.getRecords());
        return list;
    }

    @Override
    public void addCommonLine(CommonLineDto commonLineDto) {
        User user=securityUtil.getCurrUser();
        CommonLine commonLine=new CommonLine();
        ObjectUtil.copyPropertiesIgnoreNull(commonLineDto, commonLine);
        commonLine.setCompanyUserId(user.getId());
        commonLineDao.save(commonLine);
    }

    @Override
    public void deleteCommonLine(Long commonLineId) {
        if (commonLineId!=null&&commonLineId>0){
            CommonLine commonLine=commonLineDao.getOne(commonLineId);
            commonLine.setDelFlag(CommonConstant.DEL_FLAG);
            commonLineDao.save(commonLine);
        }else {
            throw new FreightException("参数不存能为空");
        }
    }
}
